package AntFarm;

import java.util.*;

public class Tile {

	
	enum TileAngle{leftDown, down, rightDown, rightUp, up, leftUp}
	
	private Set<TileContent> content;
	private Map<TileAngle, Tile> neighborTiles;
	
	
	
	public Tile(){
		this.content = new HashSet<TileContent>(); 
		this.neighborTiles = new HashMap<TileAngle, Tile>();		
	}

	
	public void addNeighborTile(Tile tile, TileAngle angle){
		
		this.neighborTiles.put(angle, tile);
		System.out.println("\t" + this.getClass().getName() + ".addNeighborTile("+ tile.getClass().getName() + ")");
	}
	
	public void addTileContent(TileContent tileContent){
		
		System.out.println("\t\t" + tileContent.getClass().getName() + " added to Tile");
		
		this.content.add(tileContent);
		tileContent.SetPositionTile(this);
		
		try {
			for (Iterator<TileContent> iterator = this.content.iterator(); iterator.hasNext();)
			{
				tileContent.visit((TileContent) iterator.next());
			}
		} catch (Exception e) {
		
		}
		
	}
	
	
	
	private List<Tile> getNeighborTilesList(){
		List<Tile> tiles = new ArrayList<Tile>();
		
		if(this.neighborTiles.get(TileAngle.up) != null)		
		tiles.add(this.neighborTiles.get(TileAngle.up));
		
		if(this.neighborTiles.get(TileAngle.down) != null)		
		tiles.add(this.neighborTiles.get(TileAngle.down));
		
		if(this.neighborTiles.get(TileAngle.leftUp) != null)		
		tiles.add(this.neighborTiles.get(TileAngle.leftUp));
		
		if(this.neighborTiles.get(TileAngle.leftDown) != null)		
		tiles.add(this.neighborTiles.get(TileAngle.leftDown));
		
		if(this.neighborTiles.get(TileAngle.rightUp) != null)		
		tiles.add(this.neighborTiles.get(TileAngle.rightUp));
		
		if(this.neighborTiles.get(TileAngle.rightDown) != null)		
		tiles.add(this.neighborTiles.get(TileAngle.rightDown));	
		return tiles;	
	}
	
	public Tile getOppositeTile(Tile tile1)
	{
		Tile tile = new Tile();
		
		TileAngle tileAngle = getAngleForTile(tile1);
		
		switch (tileAngle) {
		case down:
			if(this.neighborTiles.get(TileAngle.up) != null)		
				tile = this.neighborTiles.get(TileAngle.up);
		case up:		
				if(this.neighborTiles.get(TileAngle.down) != null)		
				tile = this.neighborTiles.get(TileAngle.down);
		case rightDown:
				if(this.neighborTiles.get(TileAngle.leftUp) != null)		
				tile = this.neighborTiles.get(TileAngle.leftUp);
		case rightUp:		
				if(this.neighborTiles.get(TileAngle.leftDown) != null)		
				tile = this.neighborTiles.get(TileAngle.leftDown);
		case leftDown:		
				if(this.neighborTiles.get(TileAngle.rightUp) != null)		
				tile = this.neighborTiles.get(TileAngle.rightUp);
		case leftUp:		
				if(this.neighborTiles.get(TileAngle.rightDown) != null)		
				tile = this.neighborTiles.get(TileAngle.rightDown);
			break;
		default:
			tile = null;
			break;
		}	
		
		return tile;
	}
	
	
	
	public List <Tile> getNeighborTiles(){
		System.out.println( this.getClass().getName() +".getNeighborTiles()");
		return getNeighborTilesList();
	}
	
	public Map<TileAngle, Tile> getNeighborTileDictionary()
	{
		return this.neighborTiles;
	}
	
	public TileAngle getAngleForTile(Tile tile)
	{
			if(tile.equals(this.neighborTiles.get(TileAngle.up)))
				return TileAngle.up;
			
			if(tile.equals(this.neighborTiles.get(TileAngle.down)))
				return TileAngle.down;
			
			if(tile.equals(this.neighborTiles.get(TileAngle.leftDown)))
				return TileAngle.leftDown;
			
			if(tile.equals(this.neighborTiles.get(TileAngle.leftUp)))
				return TileAngle.leftUp;
			
			if(tile.equals(this.neighborTiles.get(TileAngle.rightUp)))
				return TileAngle.rightUp;
			
			if(tile.equals(this.neighborTiles.get(TileAngle.rightDown)))
				return TileAngle.rightDown;
			return null;
	}

	public Set<TileContent> getTileContent(){
		System.out.println( this.getClass().getName() +".getTileContent()");
		return this.content;
	}

	public void removeTileContent(TileContent tileContent){
		if(this.content.contains(tileContent)){
			System.out.println("\t\t" + this.getClass().getName() + ".removeTileContent(" + tileContent.getClass().getName() +")");
			this.content.remove(tileContent);			
		}else{
			System.out.println("\t\t" + this.getClass().getName() + " does not contains " + tileContent.getClass().getName());
		}

	}

}